home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Demo's / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Data Manipulation / ReshapeMatrix < prev    next >
Text File  |  1996-01-29  |  2KB  |  46 lines

  1. | ReshapeMatrix
  2. | Allows you to redimension a 2-D matrix from NxM to KxL, as long as N*M == K*L
  3. | The Redimension command doesn't do this :-[
  4.  
  5. | You can set newrows to NaN to have the macro choose the right number of rows
  6. | for the given value of newcols. The reverse is true, too.
  7. | However, they can't both be NaN!
  8.  
  9. Macro ReshapeMatrix(matrix,newRows,newCols)
  10.     String matrix
  11.     Variable newRows=NaN,newCols=1    // defaults to 1 column
  12.     Prompt matrix,"matrix to reshape",popup,WaveList("*",";","")
  13.     Prompt newRows,"new number of rows, or NaN for auto"
  14.     Prompt newCols,"new number of columns, or NaN for auto"
  15.     
  16.     Silent 1;PauseUpdate
  17.     if( WaveDims($matrix) != 2 )
  18.         Abort matrix+" is not a two-dimensional wave."
  19.     endif
  20.     Variable oldRows=DimSize($matrix,0)
  21.     Variable oldCols=DimSize($matrix,1)
  22.     Variable points= oldRows*oldCols
  23.     Print "old number of rows= ",oldRows,", old number columns= ",oldCols
  24.     if( (numtype(newRows) != 0) %& (numtype(newCols) != 0) )
  25.         Abort "You must specify one or both of newrows and newcols"
  26.     endif
  27.     if( numtype(newRows) != 0 )
  28.         newRows= points/newCols
  29.     endif
  30.     if( numtype(newCols) != 0 )
  31.         newCols= points/newRows
  32.     endif
  33.     if( floor(newRows) != newRows )
  34.         Abort "non-integer new rows: "+num2str(newRows)
  35.     endif
  36.     if( floor(newCols) != newCols )
  37.         Abort "non-integer new columns: "+num2str(newCols)
  38.     endif
  39.     if( newRows * newCols != points )
  40.         Abort "number of points mismatch: new= ",newRows * newCols," old=",points
  41.     endif
  42.     Redimension/N=(points) $matrix                // redimension from matrix to vector
  43.     Redimension/N=(newRows,newCols) $matrix    // redimension from vector to matrix
  44.     Print matrix+" reshaped to ",newRows," rows by ",newCols," columns"
  45. End
  46.